<?php

class image
{
    public $path;

    public $allowSize = array(
        'width' => array(
            25, 87, 88, 100, 120, 130, 175, 200, 220, 239, 294, 300, 340, 400, 500, 930
        ),
        'high'  => array(
            25, 57, 74, 96, 100, 115, 120, 143, 180, 200, 205, 220, 225, 295,300, 400, 500, 630
        ),
    );

    public function resize($path, $width, $height)
    {
        $this->path = $path;

        if(!file_exists($path))
        {
            trigger_error('file not exist');
        }

        if(!in_array($width, $this->allowSize['width']))
        {
            trigger_error('file not width');
        }

        if(!in_array($height, $this->allowSize['high']))
        {
            trigger_error('file not height');
        }

        $type_all = array (
            0=>'UNKNOWN',
            1=>'GIF',
            2=>'JPEG',
            3=>'PNG',
            4=>'SWF',
            5=>'PSD',
            6=>'BMP',
            7=>'TIFF_II',
            8=>'TIFF_MM',
            9=>'JPC',
            10=>'JP2',
            11=>'JPX',
            12=>'JB2',
            13=>'SWC',
            14=>'IFF',
            15=>'WBMP',
            16=>'XBM',
            17=>'ICO',
            18=>'COUNT'
        );
        $old_size   = getimagesize($path);
        $img_type   = $type_all[$old_size[2]];

        $old_width  = $old_size[0];
        $old_height = $old_size[1];

        if($width==0 && $height==0) {
            $width = $old_width;
            $height = $old_height;
        } else if($width==0) {
            if($height>$old_height) {
                $height = $old_height;
            }
            $width = $height/$old_height * $old_width;
        } else if($height==0) {
            if($width>$old_width) {
                $width = $old_width;
            }
            $height = $width/$old_width * $old_height;
        }

        $dst_wh = $width/$height;
        if($old_height>($old_width/$dst_wh)) {
            $src_width = $old_width;
            $src_height = $old_width/$dst_wh;
            $src_x = 0;
            $src_y = ($old_height-$src_height)/2;
        } else {
            $src_height = $old_height;
            $src_width = $old_height*$dst_wh;
            $src_y = 0;
            $src_x = ($old_width-$src_width)/2;
        }

        switch($img_type) {
            case 'JPEG':
                $img = imagecreatefromjpeg($path);
                break;
            case 'PNG':
                $img = imagecreatefrompng($path);
                break;
            case 'GIF':
                $img = imagecreatefromgif($path);
                break;
        }

        if($src_width<$width) {
            $width = $src_width;
        }
        if($src_height<$height) {
            $height = $src_height;
        }
        //重新画
        $newimage = imagecreatetruecolor($width,$height);
        imagecopyresampled($newimage,$img,0,0,$src_x,$src_y,$width,$height,$src_width,$src_height);
        //开始缓存图片
        ob_start();
        imagejpeg($newimage);
        $data = ob_get_contents();
        ob_end_clean();
        imagedestroy($img);
        imagedestroy($newimage);
        return $data;
    }

    public function header()
    {
        header('Content-type:image/jpeg');
    }

    public function show($path, $width, $height)
    {
        $data = $this->resize($path, $width, $height);
        $this->header();
        echo $data;
    }
	
	public function savetmp($path, $width, $height)
	{
		$data = $this->resize($path, $width, $height);
		$path = '/tmp/'.md5($data).'.jpg';
		file_put_contents($path, $data);
		return $path;
	}
	
	public function deltmp($path)
	{
		@unlink($path);
	}
}